import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: xiaotutu
 * Date: 2024-12-17
 * Time: 22:39
 */
public class Test {
    public static void main(String[] args) {
        int[] array = new int[]{1,2,3};
        /*for (int i = 0; i < array.length; i++) {
            array[i] = i+1;
        }
        System.out.println(Arrays.toString(array));*/

        //Arrays.fill(array,0,3,8);//[0,3)
        func3(array);
        System.out.println(Arrays.toString(array));
    }

    public static void func3(int[] array) {
        for (int i = 0; i < array.length; i++) {
            array[i] = array[i]*2;
        }
    }
    public static void main7(String[] args) {
        int[][] array1 = new int[2][];//Java当中 不可以省略行  但是可以省略开列

        array1[0] = new int[3];
        array1[1] = new int[5];

        System.out.println(array1.length);//行的长度
        System.out.println(array1[1].length);//每一列的长度

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j <  array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
    }
    public static void main6(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        System.out.println(array1.length);//行的长度
        System.out.println(array1[1].length);//每一列的长度

        for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j <  array1[i].length; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
        System.out.println("=================");
        for(int[] tmp : array1) {
            for(int x : tmp) {
                System.out.print(x+" ");
            }
            System.out.println();
        }
        System.out.println("=============");
        String ret = Arrays.deepToString(array1);
        System.out.println(ret);
    }
    public static void main5(String[] args) {
        int[][] array1 = {{1,2,3},{4,5,6}};
        int[][] array2 = new int[2][3];
        int[][] array3 = new int[][]{{1,2,3},{4,5,6}};

        for (int i = 0; i < 2; i++) {
            for (int j = 0; j < 3; j++) {
                System.out.print(array1[i][j]+" ");
            }
            System.out.println();
        }
    }



    public static void main4(String[] args) {
        int[] array = {1,2,3,4,5};
       /* int[] copy = new int[array.length];
        System.arraycopy(array,0,copy,0,array.length);
        System.out.println(Arrays.copyOfRangtoString(copy));*/
        //int[] ret = Arrays.e(array,1,3);//[1,3)
        //System.out.println(Arrays.toString(ret));
       /* int[] ret = copy(array);
        System.out.println(Arrays.toString(ret));
        System.out.println("===========");
        int[] ret =Arrays.copyOf(array,array.length*2);
        System.out.println(Arrays.toString(ret));*/
        reverseArray(array);
        System.out.println(Arrays.toString(array));
    }
    public static void reverseArray(int[] array) {
        int i = 0;
        int j = array.length-1;
        while (i < j) {
            int tmp = array[i];
            array[i] = array[j];
            array[j] = tmp;
            i++;
            j--;
        }
    }
    public static int[] copy(int[] array) {
        int[] copyArray = new int[array.length];
        for (int i = 0; i < array.length; i++) {
            copyArray[i] = array[i];
        }
        return copyArray;
    }



    public static void main2(String[] args) {

        int a = 10;
        int[] array2 = new int[a];

        //print(array);
       /* System.out.println("前："+Arrays.toString(array));
        func1(array);
        System.out.println("后："+Arrays.toString(array));//
        int[] ret = func2();
        System.out.println("后："+Arrays.toString(ret));*/
        /*int[] array = null;
        String ret = myToString(array);
        System.out.println(ret);*/
        //int[] array3 = null;
        //System.out.println(Arrays.toString(array3));

        int[] array = {1,2,13,4,5,16,7};
        //Arrays.sort(array);
        bubbleSort(array);
        System.out.println("排序后的数组："+Arrays.toString(array));
        int index = Arrays.binarySearch(array,5);
        System.out.println(index);

        //int index = binarySearch(array,5);
        //System.out.println(index);
    }

    public static void bubbleSort(int[] array) {
        //i代表趟数
        for (int i = 0; i < array.length-1; i++) {
            //控制每趟比上一趟少1
            boolean flg = false;
            for (int j = 0; j < array.length-1; j++) {
                if(array[j] > array[j+1]) {
                    int tmp = array[j];
                    array[j] = array[j+1];
                    array[j+1] = tmp;
                    flg = true;
                }
            }
            if(flg == false) {
                break;
            }
        }
    }

    /**
     * 必须是建立在你查找的数据是有序的情况下
     * @param array
     * @param key
     * @return
     */
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length-1;
        while (left <= right) {
            int m = (left+right) / 2;
            if(array[m] > key) {
                right = m -1;
            }else if(array[m] < key) {
                left = m + 1;
            }else {
                return m;
            }
        }
        return -1;
    }

    public static int findVal(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key) {
                return i;
            }
        }
        return -1;
    }



    public static String myToString(int[] array) {
        if(array == null || array.length == 0){
            return null;
        }
        /*if(array == null) {
            return "null";
        }*/
        String ret = "[";
        for (int i = 0; i < array.length; i++) {
            ret += array[i];
            if(i != array.length-1) {
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }

    public static int[] func2() {
        int a = 10;
        int b = 20;
        return new int[]{a,b};
    }

    public static void func1(int[] array) {
        array = new int[]{9,8,7,6};
    }

    public static void print(int[] array) {
        array[0] = 8;
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        System.out.println();
    }

    public static void main1(String[] args) {
        //引用变量   引用 在这个变量当中存储的是地址
        //int[] array = {1,2,3,4,5};
        //System.out.println(array[10]);

        /*int[] array2 = null;
        System.out.println(array2.length);*/

        /*int[] array = {1,2,3,4,5};
        int[] array2 = array;

        array2[0] = 99;

        String ret1 = Arrays.toString(array);
        String ret2 = Arrays.toString(array2);
        System.out.println(ret1);
        System.out.println(ret2);*/
    }
}
